//
//  BournViewController.m
//  Anyair
//
//  Created by karlcool on 14-5-14.
//  Copyright (c) 2014年 Karlcool. All rights reserved.
//

#import "BournViewController.h"
#import "LRSegmentedControl.h"

@interface BournViewController ()

@end

@implementation BournViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    bournMapView = [[BMKMapView alloc]initWithFrame:self.view.frame];
    bournMapView.delegate = self;
    bournMapView.zoomLevel = 15;//地图级别，越高越近，最高19
    [self.view addSubview:bournMapView];
    //百度POI搜索
    searchPlace = [[BMKSearch alloc]init];
    searchPlace.delegate = self;

    LRSegmentedControl *segmented = [[LRSegmentedControl alloc]initWithFrame:CGRectMake(20, 11, 280, 28)];
    [segmented insertSegmentWithTitle:@"PM2.5" atIndex:0 animated:NO];
    [segmented insertSegmentWithTitle:@"CO2" atIndex:1 animated:NO];
    [segmented insertSegmentWithTitle:@"甲醛" atIndex:2 animated:NO];
    segmented.selectedSegmentIndex = 0;
    [segmented addTarget:self action:@selector(changeSegmented:) forControlEvents:UIControlEventValueChanged];
    
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
    backView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:backView];
    [backView addSubview:segmented];

}

- (void)viewWillAppear:(BOOL)animated {
    bournMapView.delegate = self;
    searchPlace.delegate = self;
    [self startLocation];
}

- (void)viewWillDisappear:(BOOL)animated {
    [bournMapView viewWillDisappear];
    bournMapView.delegate = nil;
    searchPlace.delegate = nil;
    [self stopLocation];
    decodeSuccess = NO;
}

- (void)releaseMemory {
    [bournMapView viewWillDisappear];
    [bournMapView removeFromSuperview];
    bournMapView = nil;
    searchPlace = nil;
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)changeSegmented:(LRSegmentedControl*)sender {
    self.dataType = sender.selectedSegmentIndex;

}


#pragma mark - 开始定位
-(void)startLocation {
    NSLog(@"进入普通定位态");
    bournMapView.showsUserLocation = NO;//先关闭显示的定位图层
    bournMapView.userTrackingMode = BMKUserTrackingModeNone;//设置定位的状态
    bournMapView.showsUserLocation = YES;//显示定位图层
    
}

//关闭定位
-(void)stopLocation {
    bournMapView.userTrackingMode = BMKUserTrackingModeNone;
    bournMapView.showsUserLocation = NO;
}


//定位成功后通过bmksearch反解码成当前地理位置，反解码完成后将调用此方法
- (void)onGetAddrResult:(BMKSearch*)searcher result:(BMKAddrInfo*)result errorCode:(int)error {
	if (error == 0) {
        bournMapView.userLocation.title = [NSString stringWithFormat:@"我在  %@  附近",result.strAddr];
        [bournMapView selectAnnotation:bournMapView.userLocation animated:YES];//设定自动显示当前位置信息
        [bournMapView setCenterCoordinate:bournMapView.userLocation.coordinate animated:YES];
        decodeSuccess = YES;
	}
}

#pragma mark - BMKMapViewDelegate -
//定位成功会调用此方法
- (void)mapView:(BMKMapView *)mapView didUpdateUserLocation:(BMKUserLocation *)userLocation
{
	if (userLocation != nil) {
        //NSLog(@"我的当前纬度%f 经度%f", userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude);
        if (bournMapView.userLocation.coordinate.longitude!=0&&bournMapView.userLocation.coordinate.latitude!=0) {
            CLLocationCoordinate2D pt = (CLLocationCoordinate2D){bournMapView.userLocation.coordinate.latitude, bournMapView.userLocation.coordinate.longitude};
            if (!decodeSuccess) {
                [searchPlace reverseGeocode:pt];
            }
            
        }
        
	}
	
}

- (void)mapView:(BMKMapView *)mapView didFailToLocateUserWithError:(NSError *)error {
    
    UIAlertView *locAlert = [[UIAlertView alloc]initWithTitle:@"定位失败" message:@"请打开您的定位功能" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil];
    [locAlert show];
}




//加载数据图钉
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
    newAnnotation = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
    
    // 设置颜色
    ((BMKPinAnnotationView*)newAnnotation).pinColor = BMKPinAnnotationColorPurple;
    // 从天上掉下效果
    ((BMKPinAnnotationView*)newAnnotation).animatesDrop = YES;
    ((BMKPinAnnotationView*)newAnnotation).image = [LRTools setAnnotationImageForValue:annotation.title AndType:self.dataType];
    ((BMKPinAnnotationView*)newAnnotation).enabled = NO;
    ((BMKPinAnnotationView*)newAnnotation).selected = NO;
    ((BMKPinAnnotationView*)newAnnotation).canShowCallout = NO;
    UILabel *valueLabel = [[UILabel alloc]initWithFrame:CGRectMake(2, 3, 30, 20)];
    valueLabel.backgroundColor = [UIColor clearColor];
    valueLabel.textColor = [UIColor whiteColor];
    valueLabel.textAlignment = NSTextAlignmentCenter;
    valueLabel.font = [UIFont systemFontOfSize:10];
    valueLabel.text = [NSString stringWithFormat:@"%@",annotation.title];
    [((BMKPinAnnotationView*)newAnnotation) addSubview:valueLabel];
    return newAnnotation;
    
    
}


#pragma mark - 根据经纬度添加图钉

- (void)addAnnotationByCoordinate:(CLLocationCoordinate2D)pt AndName:(NSString*)name {
    [bournMapView setCenterCoordinate:pt];
    
    BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
    CLLocationCoordinate2D coor;
    coor.latitude = pt.latitude;
    coor.longitude = pt.longitude;
    annotation.coordinate = coor;
    annotation.title = name;
    [bournMapView removeAnnotations:bournMapView.annotations];
    [bournMapView addAnnotation:annotation];
    [bournMapView selectAnnotation:annotation animated:YES];
}
@end
